package cn.zyq.ykb.system.version;

import cn.zyq.ykb.system.config.redis.RedisService;
import cn.zyq.ykb.system.common.CacheService;
import cn.zyq.ykb.system.config.consts.redis.RedisCommonKeys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class VersionService implements CacheService<Long> {
    @Autowired
    private RedisService redisService;

    @Override
    public String getCacheKey(String key) {
        return RedisCommonKeys.APP_VERSION_KEY;
    }

    @Override
    public Long getFromCache(String key) {
        return this.redisService.getCacheObject(this.getCacheKey(key));
    }

    @Override
    public void setToCache(Long value) {
        this.redisService.setCacheObject(this.getCacheKey(null), value);
    }

    @Override
    public void deleteForCache(String key) {
        this.redisService.deleteObject(this.getCacheKey(null));
    }

    public void setVersion() {
        long version = System.currentTimeMillis();
        this.setToCache(version);
        log.info("系统版本号：{}", version);
    }

    public Long getVersion() {
        Long version = this.getFromCache(this.getCacheKey(null));
        if (version == null) {
            return 0L;
        }
        return version;
    }
}
